﻿namespace AntdUI_HamburgerMenuTabs.Uc
{
    /// <summary>
    /// 汉堡包菜单 + 无标签页面
    /// </summary>
    public partial class HamburgerMenuPage : UserControl
    {
        public HamburgerMenuPage()
        {
            InitializeComponent();

            Load += (s, e) =>
            {
                // 加载菜单折叠/展开状态（用户控件首次显示之后调用，否则宽度计算有问题）
                LoadMenuCollapseStatus();
            };

            panelMenu.Shadow = 5;
            panelMenu.ShadowOpacityAnimation = true;
            panelMenu.ShadowOpacity = 0.2F;
            panelMenu.ShadowOpacityHover = 0.8F;

            panelPage.Shadow = 5;
            panelPage.ShadowOpacityAnimation = true;
            panelPage.ShadowOpacity = 0.2F;
            panelPage.ShadowOpacityHover = 0.8F;

            InitMenu();
        }

        /// <summary>
        /// 设置菜单项
        /// </summary>
        /// <param name="menus"></param>
        public void SetMenu(List<Common.Models.MenuItem>? menus)
        {
            menu1.SetMenu(menus);
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        private void InitMenu()
        {
            SetMenu(null);

            menu1.SetSplitterMenu(splitter1);

            menu1.Menu!.SelectChanged += (s, e) =>
            {
                if (menu1.menuSelectMode == 0)
                {
                    string title = e.Value.Text!;
                    string path = "";
                    string path2 = "";
                    Type? pageType = null;
                    if (e.Value.Tag is Common.Models.MenuItemTag tag)
                    {
                        if (tag.Path != null) path = tag.Path;
                        if (tag.Path2 != null) path2 = tag.Path2;
                        if (tag.PageType != null) pageType = tag.PageType;
                    }

                    LoadPage(title, path, path2, pageType);
                }
            };
        }

        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="title"></param>
        /// <param name="path"></param>
        /// <param name="path2"></param>
        /// <param name="pageType"></param>
        private void LoadPage(string title, string path, string path2, Type? pageType)
        {
            bool findInstance = false;

            if (panelPage.Tag is Common.Models.PageTag tag)
            {
                if (string.Equals(tag.MenuPath, path, StringComparison.OrdinalIgnoreCase))
                {
                    findInstance = true;
                }
            }

            if (!findInstance)
            {
                panelPage.Controls.Clear();
                panelPage.Tag = new Common.Models.PageTag(path);

                AntdUI.Panel panel = new()
                {
                    Dock = DockStyle.Fill,
                };

                if (pageType != null)
                {
                    object[] args = [title, path, path2];
                    UserControl pageContent;
                    try
                    {
                        pageContent = (UserControl)Activator.CreateInstance(pageType, args)!;
                    }
                    catch
                    {
                        pageContent = (UserControl)Activator.CreateInstance(pageType)!;
                    }
                    pageContent.Dock = DockStyle.Fill;
                    panel.Controls.Add(pageContent);
                }
                else
                {
                    var alert = new AntdUI.Alert()
                    {
                        TextTitle = "警告",
                        Text = "菜单未配置关联页面！",
                        Icon = AntdUI.TType.Warn,
                        Height = 100,
                        Dock = DockStyle.Top,
                    };
                    panel.Controls.Add(alert);
                }

                panelPage.Controls.Add(panel);
            }
        }

        public AntdUI.Panel? GetPanelPage()
        {
            return panelPage;
        }

        /// <summary>
        /// 菜单默认选中项
        /// </summary>
        public void MenuSelectDefault()
        {
            var item = menu1.Menu!.Items.FirstOrDefault();
            if (item != null)
            {
                string title = item.Text!;
                string path = "";
                string path2 = "";
                Type? pageType = null;
                if (item.Tag is Common.Models.MenuItemTag tag)
                {
                    if (tag.Path != null) path = tag.Path;
                    if (tag.Path2 != null) path2 = tag.Path2;
                    if (tag.PageType != null) pageType = tag.PageType;
                }

                LoadPage(title, path, path2, pageType);
            }
        }

        /// <summary>
        /// 加载菜单折叠/展开状态
        /// </summary>
        public void LoadMenuCollapseStatus()
        {
            menu1.LoadMenuCollapseStatus();
        }
    }
}